//
//  HZHotKeywordsHeaderView.m
//  app
//
//  Created by 刘华舟 on 15/5/20.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZHotKeywordsHeaderView.h"

#import "UIDevice+Resolutions.h"

static const NSInteger kHotKeywordsLabelHeight = 50;
static const NSInteger kHotKeywordsPadding = 20;
static const NSInteger kHotTitleLabelWidth = 100;
static const NSInteger kHotScrollViewHeight = 120;
static const NSInteger kHotHistoryLabelHeight = 80;

static const NSInteger kHotKeywordTextMaxWidth = 400;

@interface HZHotKeywordsHeaderView()

@property (strong, nonatomic) NSMutableArray* keywordsLabels;
@property (strong, nonatomic) NSArray* keywordsDatas;
@property (strong, nonatomic) UIScrollView *scrollView;

@property (strong, nonatomic) UILabel *hotTitleLabel;
@property (strong, nonatomic) UILabel *historyTitleLabel;

@end

@implementation HZHotKeywordsHeaderView

-(void)drawRect:(CGRect)rect
{
    
    [self drawHorizonLineLeft:[self.ubo uw:0] right:[self.ubo uw:0] at:[self.ubo uh:kHotScrollViewHeight ph:0.5]];
    [self drawHorizonLineLeft:[self.ubo uw:0] right:[self.ubo uw:0] at:[self.ubo fh:UB_HEIGHT uh:-kHotHistoryLabelHeight ph:-0.5]];
    [self drawSeperatorLineLeft:[self.ubo uw:0] right:[self.ubo uw:0]];
}

-(id)initWithFrame:(CGRect)frame
{
    frame = CGRectOf([UBO_SCREEN fx:UB_LEFT fy:UB_TOP], [UBO_SCREEN fw:UB_WIDTH uh:kHotKeywordsHeaderViewHeight]);
    self = [super initWithFrame:frame];
    if (self) {
        self.keywordsLabels = [NSMutableArray array];
        self.backgroundColor = HZ_WHITE_COLOR;
        
        //scrollView
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                                                       [self.ubo fw:UB_WIDTH uh:kHotScrollViewHeight])];
        self.scrollView.backgroundColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.scrollView];
        
        //hotTitleLabel
        self.hotTitleLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.scrollView.ubo fx:UB_LEFT fy:UB_TOP],
                                                                     [self.scrollView.ubo uw:kHotTitleLabelWidth fh:UB_HEIGHT])];
        self.hotTitleLabel.textAlignment = NSTextAlignmentLeft;
        self.hotTitleLabel.textColor = HZ_MALL_TEXT_BLACK_COLOR;
        self.hotTitleLabel.font = JKAppGlobleFontMenu;
        self.hotTitleLabel.text = @"   热搜";
        [self.scrollView addSubview:self.hotTitleLabel];
        
        //historyTitleLabel
        self.historyTitleLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_HEIGHT uy:-kHotHistoryLabelHeight],
                                                                         [self.scrollView.ubo fw:UB_WIDTH uh:kHotHistoryLabelHeight ph:-1])];
        self.historyTitleLabel.textAlignment = NSTextAlignmentLeft;
        self.historyTitleLabel.textColor = HZ_MALL_TEXT_BLACK_COLOR;
        self.historyTitleLabel.font = JKAppGlobleFontMenu;
        self.historyTitleLabel.text = @"   历史搜索";
        self.historyTitleLabel.backgroundColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR;
        [self addSubview:self.historyTitleLabel];
        
    }
    return self;
}


-(void)showKeywords:(NSArray*)keywords
{

    for(UIButton* cell in self.keywordsLabels){
        [cell removeFromSuperview];
    }
    [self.keywordsLabels removeAllObjects];
    self.keywordsDatas = keywords;
    if ([keywords isEmpty]) {
        return;
    }
    float scrollContentWidth = [self.hotTitleLabel.ubo fw:UB_WIDTH];
    NSInteger index = 0;
    
    for(NSDictionary *cate in keywords){

        //计算富文本的高度
        NSString *hotKeywordText = [cate objectForKey:APIKEY_mall_keyword nilValue:@""];
        if (hotKeywordText.length > 0) {
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            [paragraphStyle setLineSpacing:kLineSpacing];//调整行间距
            
            CGSize hotKeywordTextSize = [hotKeywordText boundingRectWithSize:[UBO_SCREEN uw:kHotKeywordTextMaxWidth uh:kHotKeywordsLabelHeight] options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:JKAppGlobleFontContentText,NSParagraphStyleAttributeName:paragraphStyle} context:nil].size;
            UIButton *label = nil;
            if (index == 0) {
                label = [[UIButton alloc] initWithFrame:CGRectOf([self.hotTitleLabel.ufo fx:UB_RIGHT fy:UB_CENTER uy:-kHotKeywordsLabelHeight/2], [self.ubo uw:2*kHotKeywordsPadding ow:hotKeywordTextSize.width uh:kHotKeywordsLabelHeight])];
            }else{
                UIButton *lastLabel = (UIButton *)[self.keywordsLabels lastObject];
                label = [[UIButton alloc] initWithFrame:CGRectOf([lastLabel.ufo fx:UB_RIGHT ux:kHotKeywordsPadding fy:UB_TOP], [self.ubo uw:2*kHotKeywordsPadding ow:hotKeywordTextSize.width uh:kHotKeywordsLabelHeight])];
            }
            
            
            [label setBackgroundColor:HZ_WHITE_COLOR];
            label.layer.masksToBounds = YES;
            label.layer.cornerRadius = [label.ubo fh:UB_HALF];
            label.layer.borderColor = HZ_MALL_BORDER_LINE_GRAY_COLOR.CGColor;
            label.layer.borderWidth = [self.ubo pw:1];
            
            label.titleLabel.font = JKAppGlobleFontContentText;
            
            [label setTitle:hotKeywordText forState:UIControlStateNormal];
            [label setTitleColor:HZ_MALL_TEXT_GRAY_COLOR forState:UIControlStateNormal];
            
            [label addTarget:self action:@selector(labelDidClicked:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.scrollView addSubview:label];
            [self.keywordsLabels addObject:label];
            
            scrollContentWidth += [label.ubo fw:UB_WIDTH uw:kHotKeywordsPadding];
            index++;
        }
    }
    
    if (scrollContentWidth < [UBO_SCREEN fw:UB_WIDTH]) {
        scrollContentWidth = [UBO_SCREEN fw:UB_WIDTH];
    }
    
    self.scrollView.contentSize = [self.scrollView.ubo ow:scrollContentWidth fh:UB_HEIGHT];
}

-(void)historyTitle:(BOOL)isHaveHis{
    if (isHaveHis) {
        self.historyTitleLabel.text = @"   历史搜索";
    }else{
        self.historyTitleLabel.text = @"   暂无历史搜索记录";
    }
}

-(void)labelDidClicked:(UIButton*)label
{
    if (self.tabSelectedCaller) {
        [self.tabSelectedCaller call:label.currentTitle];
    }
}


@end
